package edu.neu.swc.cloud.controller;

import com.alibaba.fastjson.JSON;
import edu.neu.swc.cloud.domain.Admin;
import edu.neu.swc.cloud.domain.Factory;
import edu.neu.swc.cloud.domain.QueryInfo;
import edu.neu.swc.cloud.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    @RequestMapping("/list")
    public List<Admin> list(){
        return adminService.findAll();
    }

    @RequestMapping("/addAdmin")
    public String save(@RequestBody Admin admin){
        admin.setAd_type(1);
        System.out.println(admin);
        int status = adminService.addAdmin(admin);
        if(status == 1){
            return "success";
        }else{
            return "error";
        }
    }

    @RequestMapping("/update")
    public String update(Admin admin){
        adminService.updateAdmin(admin);
        //添加一个空格
        return "redirect:/admin/list";
    }

    @RequestMapping("/deleteAdmin")
    public String delete(int id){
        System.out.println("delete id:" + id);
        adminService.deleteAdmin(id);
        return "success";
    }

    @RequestMapping("/search")
    public List<Admin> search(Admin admin){
        List<Admin> result = adminService.findAdminByCon(admin);
        return result;
    }

    @RequestMapping("/login")
    public Map<String,Object> logon(@RequestBody Admin admin){

        Map<String,Object> map = new HashMap<>();
        System.out.println(admin);

        Admin will = adminService.logon(admin.getAd_username(),admin.getAd_password());
        System.out.println(will);
        if(will == null){
            map.put("flag","error");
        }else if(will.getAd_type() == 1){
            //是系统管理员，直接转到管理员界面
            map.put("flag","ok");
            map.put("type","super");
        }else if(will.getAd_type() == 2){
            //云工厂管理员，返回对应的工厂即可
            Factory myFactory = adminService.ownFactory(will);
            map.put("flag","ok");
            map.put("type","factory");
            map.put("fac_id",myFactory.getFa_id());
        }else if(will.getAd_type() == 3){
            map.put("flag","ok");
            map.put("type","agent");
        }else{
            //系统出现错误，与登陆失败一并处理
            map.put("flag","error");
        }
        return map;
    }

    @RequestMapping("/register")
    public String register(@RequestBody Admin admin){
        String flag;
        System.out.println(admin);
        System.out.println("register called");
        int status = adminService.register(admin);
        if(status == 1){
            //说明注册信息正确，可以直接注册，重定向到登陆页面
            flag = "success";
        }else{
            flag = "error";
        }
        return flag;
    }

    @RequestMapping("/adminList")
    public String adminList(QueryInfo queryInfo){
        int numbers = adminService.getAdminCounts("%"+queryInfo.getQuery()+"%");// 获取数据总数
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<Admin> adminList = adminService.findAdminByQuery("%"+queryInfo.getQuery()+"%",pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",adminList);
        System.out.println("总条数："+numbers);
        System.out.println(adminList);
        return JSON.toJSONString(res);
    }

    @RequestMapping("/getUpdate")
    public String getUpdate(int id){
        Admin updateAdmin = adminService.findAdminById(id);
        System.out.println("id:"+id);
        return JSON.toJSONString(updateAdmin);
    }

    @RequestMapping("/editAdmin")
    public String editAdmin(@RequestBody Admin admin){
        System.out.println(admin);
        adminService.updateAdmin(admin);
        return "success";
    }
}
